package org.advanced.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author John.zhang
 * @version Id: NioFirst, v 1.0 2020/12/15 15:14 ZhiYong Exp $
 * 初步理解基于NIO实现一个文件复制(New IO)
 * 面向缓冲区
 * 缓存并支持数据移动
 *
 */
public class NioFirst {

    public static void main(String[] args) {
        try {
            FileInputStream fileInputStream = new FileInputStream(new File("C:/data/io/io.txt"));
            FileOutputStream fileOutputStream = new FileOutputStream(new File("C:/data/io/io_cp.txt"));
            //NIO channel - buffer
            FileChannel inputChannel = fileInputStream.getChannel();
            FileChannel outputChannel = fileOutputStream.getChannel();
            //初始化一个缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            //读取数据到缓冲区
            inputChannel.read(byteBuffer);
            //从读转化为写
            byteBuffer.flip();
            outputChannel.write(byteBuffer);
            byteBuffer.clear();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
